<template>
  <div class="wrapper">
    <img
      class="wrapper__img"
      src="http://www.dell-lee.com/imgs/vue3/user.png"
    />
    <div class="wrapper__content">
      <input
        type="text"
        class="wrapper__content__id"
        placeholder="请输入用户名"
        v-model="username"
      />
    </div>
    <div class="wrapper__content">
      <input
        type="password"
        class="wrapper__content__password"
        placeholder="请输入密码"
        v-model="password"
      />
    </div>
    <div class="wrapper__content">
      <input
        type="password"
        class="wrapper__content__password"
        placeholder="确认密码"
        v-model="passwords"
      />
    </div>
    <div class="wrapper__button" @click="handleRegister">注册</div>
    <div class="wrapper__link" @click="handleLoginClick">已有帐号去注册</div>
  </div>
  <Toast v-if="show" :message="toastMessage" />
</template>
<script>
import { useRouter } from "vue-router";
import { post } from "../../utils/request.js";
import { reactive, toRefs } from "vue";
import Toast, { useToastEffect } from "../../components/toast/Toast.vue";
//注册相关逻辑
const useRegisterEffect = (ToastShow) => {
  const router = useRouter();
  const Url = "api/user/register";
  const data = reactive({
    username: "",
    password: "",
    passwords: "",
  });
  async function handleRegister() {
    try {
      if ((data.username !== "", data.password !== "")) {
        if (data.password === data.passwords) {
          const result = await post(Url, {
            username: data.username,
            password: data.password,
          });
          if (result?.data?.errno === 0) {
            router.push("/login");
          } else {
            ToastShow("注册失败");
          }
        } else {
          ToastShow("两次密码不一致");
        }
      } else {
        ToastShow("用户名和密码不能为空");
      }
    } catch {
      ToastShow("请求失败");
    }
  }
  const { username, password, passwords } = toRefs(data);
  return { username, password, passwords, handleRegister };
};
//登录跳转
const handleRouterClick = () => {
  const router = useRouter();
  function handleLoginClick() {
    router.push("/login");
  }
  return { handleLoginClick };
};
export default {
  name: "LoginPage",
  components: { Toast },
  setup() {
    const { ToastShow, toastMessage, show } = useToastEffect();
    const { username, password, passwords, handleRegister } =
      useRegisterEffect(ToastShow);
    const { handleLoginClick } = handleRouterClick();
    return {
      handleRegister,
      handleLoginClick,
      username,
      password,
      passwords,
      toastMessage,
      show,
    };
  },
};
</script>
<style lang="scss" scoped>
@import "../../style/viriables.scss";
.wrapper {
  padding: 0 0.4rem;
  &__img {
    display: block;
    padding-top: 1.2rem;
    width: 0.66rem;
    height: 0.66rem;
    margin: 0 auto 0.4rem;
  }
  &__content {
    box-sizing: border-box;
    width: 100%;
    height: 0.48rem;
    background-color: #f9f9f9;
    margin-bottom: 0.16rem;
    padding-left: 0.16rem;
    border: 0.01rem solid rgba(0, 0, 0, 0.1);
    border-radius: 0.06rem;
    &__id,
    &__password {
      border: 0;
      outline: none;
      background: none;
      width: 100%;
      // height: 100%;
      margin-top: 0.1rem;
      font-size: 0.16rem;
      color: $content-notice-fontcolor;
    }
  }
  &__button {
    width: 100%;
    height: 0.48rem;
    background-color: $color-0091ff;
    color: $color-fff;
    box-shadow: 0 0.04rem 0.08rem 0 rgba(0, 145, 255, 0.32);
    border-radius: 0.04rem;
    line-height: 0.48rem;
    text-align: center;
    font-size: 0.16rem;
    margin-top: 0.32rem;
  }
  &__link {
    margin-top: 0.16rem;
    font-size: 0.14rem;
    color: $content-notice-fontcolor;
    text-align: center;
  }
}
</style>
